package com.hieu.sms.web.mb;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.annotation.PostConstruct;

import org.primefaces.event.CloseEvent;
import org.primefaces.event.SelectEvent;

import com.hieu.sms.model.Clazz;
import com.hieu.sms.model.Course;
import com.hieu.sms.model.Department;
import com.hieu.sms.model.Major;
import com.hieu.sms.services.InformationServices;

@ManagedBean(name = "mbDepartment")
@ViewScoped
public class MbDepartment extends
		AbstractManagedBeanSimple<Department, Department> implements
		Serializable {

	private static final long serialVersionUID = 1L;
	
	private List<Major> majors ;
	private Major majorSelected;
	
	private Major major;
	
	private Map<String, String> majorMap;
	
	//private Map<String,String> depMap;
	
	private List<Course> courses;
	
	private Course courseSelected;
	private Course course;

	@ManagedProperty("#{informationServices}")
	private InformationServices informationServices;

	public void setInformationServices(InformationServices informationServices) {
		this.informationServices = informationServices;
	}

	public MbDepartment() {
		super();
	}

	@PostConstruct
	public void init() {
		this.selected = new Department();
		this.major = new Major();
		this.course = new Course();
		this.clazz = new Clazz();
		search();
	}

	@Override
	protected List<Department> searchListCallback(Department filter)
			throws Exception {
		List<Department> departments = informationServices.list();
		/*
		depMap = new HashMap<String, String>(departments.size());
		for(Department department: departments){
			depMap.put(department.getName(), department.getDepId());
		}
		*/
		return departments;
	}

	@Override
	protected String deleteCallback(Department item) throws Exception {
		informationServices.delete(item);

		return null;
	}

	@Override
	protected String addCallback(Department item) throws Exception {
		informationServices.add(item);
		return null;
	}

	@Override
	protected String modifyCallback(Department item) throws Exception {
		informationServices.update(item);

		return null;
	}

	@Override
	protected String deleteCallback(String id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String getId(Department item) {
		// TODO Auto-generated method stub
		return null;
	}

	public void onRowSelect(SelectEvent selectEvent) throws Exception {
		editMode = true;
		String depId = selected.getDepId();
		searchMajor(depId);
		searchCourse(depId);
		searchClazz(depId);
		major = new Major();
		major.setDepId(depId);
		clazz = new Clazz();
		clazz.setDepId(depId);
	}
	
	private void searchMajor(String depId) throws Exception{
		majors = informationServices.findMajor(selected.getDepId());
		majorMap= new HashMap<String, String>(majors.size());
		
		for(Major major: majors){
			majorMap.put(major.getName(), major.getMajorId());
		}
	}
	
	private void searchCourse(String depId)throws Exception{
		courses = informationServices.findCourse(depId);
	}
	
	
	public void prepareAdd() {
		setItem(new Department());
	}

	public void prepareEdit() {
		setItem(selected);
	}

	public void handleClose(CloseEvent event) {

	}
	
	public void handleMajorWindowClose(CloseEvent event) {

	}
	
	public void handleCourseWindowClose(CloseEvent event){
		
	}

	public List<Major> getMajors() {
		return majors;
	}

	public Major getMajorSelected() {
		return majorSelected;
	}

	public void setMajorSelected(Major majorSelected) {
		this.majorSelected = majorSelected;
	}

	public void setMajors(List<Major> majors) {
		this.majors = majors;
	}

	
	public Major getMajor() {
		return major;
	}
	
	public Map<String, String> getMajorMap() {
		return majorMap;
	}

	public void setMajor(Major major) {
		this.major = major;
	}


	public void prepareAddMajor(){
		major = new Major();
		if(selected !=null){
			major.setDepId(selected.getDepId());
		}
	}
	
	public void prepareEditMajor(){
		major = majorSelected;
	}
	
	public void doDeleteMajor() throws Exception{
		informationServices.delete(majorSelected);
		majors = informationServices.findMajor(selected.getDepId());
		majorSelected = null;
		
	}
	
	public void doAddMajor() throws Exception{
		informationServices.add(major);
		searchMajor(selected.getDepId());
		majorSelected = null;
	}
	public void onMajorRowSelect(SelectEvent event){
		
	}

	public List<Course> getCourses() {
		return courses;
	}

	public void setCourses(List<Course> courses) {
		this.courses = courses;
	}

	public Course getCourseSelected() {
		return courseSelected;
	}

	public void setCourseSelected(Course courseSelected) {
		this.courseSelected = courseSelected;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}
	
	public void prepareAddCourse(){
		course = new Course();
		if(selected!=null){
			course.setDepId(selected.getDepId());
		}
	}
	
	public void prepareEditCourse(){
		course = courseSelected;
	}
	public void doDeleteCourse() throws Exception{
		informationServices.delete(courseSelected);
		searchCourse(selected.getDepId());
		courseSelected = null;
	}
	public void doAddCourse() throws Exception{
		informationServices.add(course);
		searchCourse(selected.getDepId());
		courseSelected = null;
	}
	
	public void onCourseRowSelect(SelectEvent event){
		
	}
	
	
	private List<Clazz> clazzs;
	private Clazz clazzSelected;
	private Clazz clazz;
	
	public List<Clazz> getClazzs() {
		return clazzs;
	}

	public void setClazzs(List<Clazz> clazzs) {
		this.clazzs = clazzs;
	}

	public Clazz getClazzSelected() {
		return clazzSelected;
	}

	public void setClazzSelected(Clazz clazzSelected) {
		this.clazzSelected = clazzSelected;
	}

	public Clazz getClazz() {
		return clazz;
	}

	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}

	public void prepareAddClass(){
		clazz = new Clazz();
		if(selected !=null){
			clazz.setDepId(selected.getDepId());
		}
	}
	
	public void prepareEditClass(){
		clazz = clazzSelected;
	}
	
	public void doDeleteClass() throws Exception{
		informationServices.delete(clazzSelected);
		searchClazz(clazzSelected.getDepId());
		clazzSelected = null;
	}
	public void handleClassWindowClose(){
		
	}
	
	private void searchClazz(String depId) throws Exception{
		clazzs = informationServices.findClazz(depId);
		
	}
	public void doAddClass()throws Exception{
		informationServices.add(clazz);
		searchClazz(selected.getDepId());
		clazzSelected = null;
		
	}
	public void onClassRowSelect(){
		
	}
}
